using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyFrog : Enemy
{
    private Rigidbody2D rb;
    //private Animator Anim;
    public float speed;
    public Transform leftPoint, rightPoint;

    public bool FaceLeft = true;
    private float leftx,rightx;
    private bool isGround;
    public Transform groundCheck;
    public LayerMask ground;
    // Start is called before the first frame update
    protected override void Start()
    {
        base.Start();
        rb = GetComponent<Rigidbody2D>();
        //Anim = GetComponent<Animator>();
        leftx = leftPoint.transform.position.x;
        rightx = rightPoint.transform.position.x;
        Destroy(leftPoint.gameObject);
        Destroy(rightPoint.gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void FixedUpdate(){
      isGround = Physics2D.OverlapCircle(groundCheck.position, 0.1f, ground);
      //Movement();
      SwitchAmin();
      if(transform.position.x <= leftx){
        transform.localScale = new Vector3(-1,1,1);
        FaceLeft = false;
      }
      if(transform.position.x >= rightx){
        transform.localScale = new Vector3(1,1,1);
        FaceLeft = true;
      }
    }

    void Movement(){
      if(isGround){
        if(FaceLeft){
          rb.velocity = new Vector2(-speed, 10);
          
        }else{
          rb.velocity = new Vector2(speed, 10);
        
        }
      }
      

    }

    void SwitchAmin(){
      if(isGround){
        Anim.SetBool("fail", false);
        Anim.SetBool("jump", false);
      }else if (!isGround && rb.velocity.y>0){
        Anim.SetBool("jump", true);
      }else if(rb.velocity.y<0){
        Anim.SetBool("jump", false);
        Anim.SetBool("fail", true);
      }
    }
}
